import request from '../instance'
import { API_ROUTES } from '../config'

export interface Recipe {
  id: string
  name: string
  type: 'meat' | 'vegetable' | 'soup'
  ingredients: string[]
  steps: string[]
  mealTypes: ('breakfast' | 'lunch' | 'dinner')[]
  spicyLevel: number
  cookingTime: number
  tags: string[]
  imageUrl: string
  createdAt: string
}

export interface DailyRecommendationParams {
  spicy: boolean
  spicyLevel: number
  allergies: string
  familyId?: string
}

export const recipeApi = {
  // 上传图片
  uploadImage: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return request.post<any, { imageUrl: string }>(
      API_ROUTES.UPLOAD_IMAGE,
      formData,
      {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
    )
  },

  // 创建菜谱
  createRecipe: (data: Omit<Recipe, 'id' | 'createdAt'>) => {
    return request.post<any, Recipe>(API_ROUTES.CREATE_RECIPE, data)
  },

  // 更新菜谱
  updateRecipe: (recipeId: string, data: Partial<Recipe>) => {
    return request.put<any, Recipe>(API_ROUTES.UPDATE_RECIPE(recipeId), data)
  },

  // 获取菜谱详情
  getRecipe: (recipeId: string) => {
    return request.get<any, Recipe>(API_ROUTES.GET_RECIPE(recipeId))
  },

  // 获取所有菜谱
  getAllRecipes: () => {
    return request.get<any, Recipe[]>(API_ROUTES.GET_ALL_RECIPES)
  },

  // 获取每日推荐
  getDailyRecommendation: (params: DailyRecommendationParams) => {
    return request.get(API_ROUTES.GET_DAILY_RECOMMENDATION, { params })
  },

  // 获取周计划
  getWeeklyPlan: (familyId?: string) => {
    return request.get(API_ROUTES.GET_WEEKLY_PLAN, {
      params: { familyId }
    })
  }
}